package com.ruoyi.web.controller.modules;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.LzmxEnum;
import com.ruoyi.common.enums.ZdjcEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.modules.bean.*;
import com.ruoyi.modules.mapper.TApptokenMapper;
import com.ruoyi.modules.mapper.TOptionMapper;
import com.ruoyi.modules.mapper.TRwbMapper;
import com.ruoyi.modules.service.*;
import com.ruoyi.modules.vo.SelectUserVo;
import com.ruoyi.modules.vo.XdrwVo;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yjgl")
public class RwbController {

    @Autowired
    private TRwbService tRwbService;

    @Autowired
    private TFkbService tFkbService;

    @Autowired
    private TFjbService tFjbService;

    @Autowired
    private TUserService tUserService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private TOptionMapper tOptionMapper;
    @Autowired
    private TApptokenMapper tApptokenMapper;
    @Autowired
    private TRwbMapper tRwbMapper;

    @PostMapping("/addxdwj")
    public AjaxResult addxdwj(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.addxdwj(xdrwVo);
    }

    @PostMapping("/messageSendByLimit")
    public AjaxResult messageSendByLimit(@RequestBody XdrwVo xdrwVo) {
        return tRwbService.messageSendByLimit(xdrwVo);
    }

    @Anonymous
    @PostMapping("/ssmSendByLimit")
    public AjaxResult ssmSendByLimit(@RequestBody XdrwVo xdrwVo) {
        return tRwbService.ssmSendByLimit(xdrwVo);
    }


    @PostMapping("/addZjxdwj")
    public AjaxResult addZjxdwj(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.addZjxdwj(xdrwVo);
    }
    @Anonymous
    @PostMapping("/addAppZjxdwj")
    public AjaxResult addAppZjxdwj(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.addAppZjxdwj(xdrwVo);
    }

    @PostMapping("/tranXdrw")
    public AjaxResult tranXdrw(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.tranXdrw(xdrwVo);
    }

    @Anonymous
    @PostMapping("/tranAppXdrw")
    public AjaxResult tranAppXdrw(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.tranAppXdrw(xdrwVo);
    }

    @PostMapping("/tranXdrwToUser")
    public AjaxResult tranXdrwToUser(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.tranXdrwToUser(xdrwVo);
    }

    @Anonymous
    @PostMapping("/tranAppXdrwToUser")
    public AjaxResult tranAppXdrwToUser(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        return tRwbService.tranAppXdrwToUser(xdrwVo);
    }

    @PostMapping("/updateRwb")
    public AjaxResult updateRwb(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        try {
            tRwbService.updateRwb(xdrwVo);
        } catch (Exception e) {
            response.setStatus(HttpStatus.ERROR);
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @PostMapping("/updateZjxdRwb")
    public AjaxResult updateZjxdRwb(@RequestBody XdrwVo xdrwVo, HttpServletResponse response) {
        try {
            tRwbService.updateZjxdRwb(xdrwVo);
        } catch (Exception e) {
            response.setStatus(HttpStatus.ERROR);
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Anonymous
    @GetMapping("/getRwPage")
    public TableDataInfo getRwPage(@RequestParam Map<String, String> params) {
        PageInfo<TRwb> rwPage = tRwbService.getRwPage(params);
        return getDataTable(rwPage);
    }


    @Anonymous
    @GetMapping("/getZjxdRwPage")
    public TableDataInfo getZjxdRwPage(@RequestParam Map<String, Object> params) {
        PageInfo<TZjxdRwb> rwPage = tRwbService.getZjxdRwPage(params);
        return getDataTable(rwPage);
    }
    @Anonymous
    @GetMapping("/getAppZjxdRwPage")
    public TableDataInfo getAppZjxdRwPage(@RequestParam Map<String, Object> params) {
        PageInfo<TZjxdRwb> rwPage = tRwbService.getAppZjxdRwPage(params);
        return getDataTable(rwPage);
    }

    @Anonymous
    @GetMapping("/getRwbInfo")
    public AjaxResult getRwbInfo(String sid) {
        TRwb tRwb = tRwbService.getRwbInfo(sid);
        return AjaxResult.success(tRwb);
    }

    @Anonymous
    @GetMapping("/getAppUserListByZjxdId")
    public AjaxResult getAppUserListByZjxdId(String sid) {
        TZjxdRwb tRwb = tRwbService.getAppUserListByZjxdId(sid);
        return AjaxResult.success(tRwb);
    }

    @Anonymous
    @GetMapping("/getZjxdRwbInfo")
    public AjaxResult getZjxdRwbInfo(String sid) {
        TZjxdRwb tZjxdRwb = tRwbService.getZjxdRwbInfo(sid);
        return AjaxResult.success(tZjxdRwb);
    }

    @Anonymous
    @GetMapping("/getAppZjxdRwbInfo")
    public AjaxResult getAppZjxdRwbInfo(@RequestParam String sid, @RequestParam String username) {
        TZjxdRwb tZjxdRwb = tRwbService.getAppZjxdRwbInfo(sid, username);
        return AjaxResult.success(tZjxdRwb);
    }


    @Anonymous
    @GetMapping("/getChildOptions")
    public TableDataInfo getChildOptions(@RequestParam Map<String, String> params) {
        PageInfo<Map> childOptions = tRwbService.getChildOptions(params);
        return getDataTable(childOptions);
    }


    @Anonymous
    @GetMapping("/getUserByRwId")
    public TableDataInfo getUserByRwId(@RequestParam Map<String, String> params) {
        PageInfo<TUserFk> tUsers = tRwbService.getUserByRwId(params);
        return getDataTable(tUsers);
    }

    @Anonymous
    @GetMapping("/getZjxdUserByRwId")
    public TableDataInfo getZjxdUserByRwId(@RequestParam Map<String, String> params) {
        PageInfo<TOption> toptions = tRwbService.getZjxdUserByRwId(params);
        return getDataTable(toptions);
    }

    @PostMapping("/export")
    public void export(@RequestParam Map<String, String> params, HttpServletResponse response) {
        tRwbService.export(params, response);
    }

    @GetMapping("/delRwByIds")
    public AjaxResult delRwByIds(XdrwVo xdrwVo) {
        try {
            tRwbService.delRwByIds(xdrwVo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }
    @GetMapping("/delZjxdRwByIds")
    public AjaxResult delZjxdRwByIds(XdrwVo xdrwVo) {
        try {
            tRwbService.delZjxdRwByIds(xdrwVo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    protected TableDataInfo getDataTable(Page<?> page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    protected TableDataInfo getDataTable(PageInfo<?> pageInfo) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageInfo.getList());
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }

    // 根据企业账号获取反馈信息
    @GetMapping("/getFkxxByUserId")
    public AjaxResult getFkxxByUserId(@RequestParam Map<String, String> params) {
        return tRwbService.getFkxxByUserId(params);
    }

    // 根据企业账号获取反馈历史信息
    @GetMapping("/getHistoryFkxxByUserId")
    public AjaxResult getHistoryFkxxByUserId(@RequestParam Map<String, String> params) {
        return tRwbService.getHistoryFkxxByUserId(params);
    }

    // 查看反馈详情
    @GetMapping("/getFkxxInfo")
    public AjaxResult getFkxxInfo(@RequestParam Map<String, String> params) {
        return tRwbService.getFkxxInfo(params);
    }

    @Anonymous
    @GetMapping("/getFkIdByRwbAndUserId")
    public AjaxResult getFkIdByRwbAndUserId(@RequestParam Map<String, String> params) {
        return tRwbService.getFkIdByRwbAndUserId(params);
    }

    @Anonymous
    @GetMapping("/getFkFj")
    public AjaxResult getFkFj(@RequestParam Map<String, String> params) {
        return tRwbService.getFkFj(params);
    }
    @Anonymous
    @GetMapping("/getZjxdFkFj")
    public AjaxResult getZjxdFkFj(@RequestParam Map<String, String> params) {
        return tRwbService.getZjxdFkFj(params);
    }

    // 查看反馈详情
    @PostMapping("/submitFk")
    public AjaxResult submitFk(@RequestBody XdrwVo xdrwVo) {
        return tRwbService.submitFk(xdrwVo);
    }
    // 查看反馈详情
    @Anonymous
    @PostMapping("/submitAppZjxdFk")
    public AjaxResult submitAppZjxdFk(@RequestBody XdrwVo xdrwVo) {
        return tRwbService.submitAppZjxdFk(xdrwVo);
    }

//    @GetMapping("/getTxlDeptByid")
//    public AjaxResult getTxlDeptByid(String id) {
//        return tRwbService.getTxlDeptByid(id);
//    }

    @GetMapping("/getTxlTab")
    public AjaxResult getTxlTab() {
        return tRwbService.getTxlTab();
    }

//    @PostMapping("/getSelectUser")
//    public AjaxResult getSelectUser(@RequestBody SelectUserVo selectUserVo) {
//        return tRwbService.getSelectUser(selectUserVo);
//    }

    @GetMapping("/getSelectUserPage")
    public TableDataInfo getSelectUserPage(@RequestParam Map<String, String> params) {
        return tRwbService.getSelectUserPage(params);
    }
    @GetMapping("/getSelectZjxdUserPage")
    public TableDataInfo getSelectZjxdUserPage(@RequestParam Map<String, String> params) {
        Page<TUser> selectUserPage = tRwbService.getSelectZjxdUserPage(params);
        return getDataTable(selectUserPage);
    }

    @PostMapping("/delSelectUser")
    public AjaxResult delSelectUser(@RequestBody XdrwVo xdrwVo) {
//        tUserService.removeBatchByIds(xdrwVo.getIds());
        List<String> ids = xdrwVo.getIds();
        tRwbService.delSelectUser(xdrwVo);

        return AjaxResult.success();
    }

    @Anonymous
    @GetMapping("/getDeptByUserId")
    public AjaxResult getDeptByUserId(XdrwVo xdrwVo) {
        return tRwbService.getDeptByUserId(xdrwVo);
    }


    @Anonymous
    @PostMapping("/tranMessageByUser")
    public AjaxResult tranMessageByUser(@RequestBody XdrwVo xdrwVo) {
        return tRwbService.tranMessageByUser(xdrwVo);
    }

    @Anonymous
    @GetMapping("/updateIsTranBySid")
    public AjaxResult updateIsTranBySid(String sid,Boolean isUpdate,String ly){
        // 修改任务表isTran为1
       return tRwbService.updateIsTranBySid(sid,isUpdate,ly);
    }

    @Anonymous
    @GetMapping("/getLoginUserInfoByAppToken")
    public AjaxResult getLoginUserInfoByAppToken(String appToken){
        return tRwbService.getLoginUserInfoByAppToken(appToken, LzmxEnum.YJFK_APPCODE.getValue());
    }

    @Anonymous
    @GetMapping("/getDepartmentList")
    public AjaxResult getDepartmentList(@RequestParam Map<String, String> params){
        return tRwbService.getDepartmentList(params);
    }

    @Anonymous
    @GetMapping("/getDepartmentList1")
    public AjaxResult getDepartmentList1(@RequestParam Map<String, String> params){
        return tRwbService.getDepartmentList1(params);
    }
    @Anonymous
    @PostMapping("/andUserByDeptAndLaber")
    public AjaxResult andUserByDeptAndLaber(@RequestBody SelectUserVo selectUserVo){
        return tRwbService.andUserByDeptAndLaber(selectUserVo);
    }

    @Anonymous
    @GetMapping("/getUserLabelList")
    public AjaxResult getUserLabelList(@RequestParam Map<String, String> params){
        return tRwbService.getUserLabelList(params);
    }
    @GetMapping("/updateCysjByfkId")
    public AjaxResult updateCysjByfkId(@RequestParam Map<String, String> params){
        return tRwbService.updateCysjByfkId(params);
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String workid) throws Exception
    {
        ExcelUtil<TUserExcel> util = new ExcelUtil<TUserExcel>(TUserExcel.class);
        List<TUserExcel> userList = util.importExcel(file.getInputStream());
       return tRwbService.importData(userList,workid);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TUserExcel> util = new ExcelUtil<TUserExcel>(TUserExcel.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Anonymous
    @GetMapping("/queryUserByUsername")
    public AjaxResult queryUserByUsername(@RequestParam String username){
        return tRwbService.queryUserByUsername(username);
    }

    @GetMapping("/getCityLevel")
    public AjaxResult getCityLevel(@RequestParam Map<String,Object> params){
        return deptService.getCityLevel(params);
    }

    @Anonymous
    @GetMapping("/getAppCityLevel")
    public AjaxResult getAppCityLevel(@RequestParam Map<String,Object> params){
        return deptService.getAppCityLevel(params);
    }

    @GetMapping("/getSonDeptOptionInfo")
    public AjaxResult getSonDeptOptionInfo(@RequestParam Map<String,String> params){
        return tRwbService.getSonDeptOptionInfo(params);
    }
    @GetMapping("/getSonDeptOptionInfoById")
    public AjaxResult getSonDeptOptionInfoById(@RequestParam Map<String,String> params){
        return tRwbService.getSonDeptOptionInfoById(params);
    }

    @Anonymous
    @PostMapping("/addApptoken")
    public AjaxResult addApptoken(@RequestBody TApptoken apptoken){
        try {
            QueryWrapper<TApptoken> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",apptoken.getUserName());
            List<TApptoken> tApptokens = tApptokenMapper.selectList(queryWrapper);
            if (tApptokens.size() > 0) {
                tApptokens.get(0).setAppToken(apptoken.getAppToken());
                tApptokenMapper.updateById(tApptokens.get(0));
            } else {
                tApptokenMapper.insert(apptoken);
            }
            return AjaxResult.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.success(e.getMessage());
        }
    }

    @Anonymous
    @GetMapping("/getSxlxCount")
    public AjaxResult getSxlxCount(@RequestParam Map<String,String> params){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<Map<String, Integer>> map = tRwbMapper.getSxlxCount(params);
        return AjaxResult.success(map);
    }

    /**
     * 获取我下达的事项类型数量
     * @param params
     * @return
     */
    @GetMapping("/getMySendMatter")
    public AjaxResult getMySendMatter(@RequestParam Map<String,String> params){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        params.put("xdrId",String.valueOf(loginUser.getUserId()));
        List<Map<String, Integer>> map = tRwbMapper.getSxlxCount(params);
        return AjaxResult.success(map);
    }

    @Anonymous
    @GetMapping("/getSxlxCountData")
    public AjaxResult getSxlxCountData(@RequestParam Map<String,String> params){
        List<Map<String, Integer>> map = tRwbService.getSxlxCountData(params);
        return AjaxResult.success(map);
    }

    @Anonymous
    @GetMapping("/getXdbmCount")
    public AjaxResult getXdbmCount(@RequestParam Map<String,String> params){
        List<Map<String, Integer>> map = tRwbMapper.getXdbmCount(params);
        return AjaxResult.success(map);
    }

    @GetMapping("/delRedisUserByKey")
    public AjaxResult delRedisUserByKey(@RequestParam Map<String,String> params){
        return tRwbService.delRedisUserByKey(params);
    }

    @GetMapping("/getXdrwByWorkid")
    public AjaxResult getXdrwByWorkid(@RequestParam Map<String,String> params){
        return tRwbService.getXdrwByWorkid(params);
    }

    @GetMapping("/getUserDepartment")
    public AjaxResult getUserDepartment(@RequestParam Map<String,String> params){
        return tRwbService.getUserDepartment(params);
    }

    // 获取我的待办、已办
    @GetMapping("/getMyToDoDone")
    public AjaxResult getMyToDoDone(@RequestParam Map<String,String> params){
        return tRwbService.getMyToDoDone(params);
    }

    // 获取我的待办
    @GetMapping("/getMyToDo")
    public AjaxResult getMyToDo(@RequestParam Map<String,String> params){
        return tRwbService.getMyToDo(params);
    }

    @GetMapping("/getInstitutionCount")
    public AjaxResult getInstitutionCount(@RequestParam Map<String,String> params){
        return tRwbService.getInstitutionCount(params);
    }

    @GetMapping("/getYfbxx")
    public TableDataInfo getYfbxx(@RequestParam Map<String,String> params){
        return getDataTable(tRwbService.getYfbxx(params));
    }

    /**
     * @author zhouzhanwei
     * @date 2024/11/17 17:47
     * 获取统计图下拉框内容
     */
    @GetMapping("/getStatisticsSelectData")
    public AjaxResult getStatisticsSelectData(@RequestParam Map<String,Object> params){
        return tRwbService.getStatisticsSelectData(params);
    }

}
